package com.cxhome.cxinfox.cxmenu;

import java.util.List;

import com.jfinal.plugin.activerecord.Page;
import com.traffic.managent.annotation.ControllerBind;
import com.traffic.managent.base.CxBaseController;
import com.traffic.managent.model.system.Menu;
import com.traffic.managent.model.system.Module;
import com.traffic.managent.util.SQLUtils;
import com.traffic.managent.util.UtilsUtils;

/**
 * 模块
 * 
 */
@ControllerBind(controllerKey = "/system/cxmodule")
public class ModuleController extends CxBaseController {

	private static final String modulePath = "/pages/system/cxmodule/cxmodule_";

	public void index() {
		list();
	}

	public void list() {
		Module model = getModelByAttr(Module.class);
		Page<Module> page = ModuleService.service.paginate(model, getPaginator());

		setAttr("page", page);
		setAttr("attr", model);
		List<Module> departments = Module.dao.find("select * from cx_module  ");
		setAttr("cxmodules", departments);
		render(modulePath + "list.html");
	}
	
	
	/**
	 * 跳转新增页面
	 */
	public void add() {
		// 获取页面信息,设置目录传入
		setAttr("selectSystems", new SystemsService().selectSystems(""));
		setAttr("selectModule", new ModuleService().selectModule(""));
		render(modulePath + "add.html");
	}

	/**
	 * 查看详情
	 */
	public void view() {
		Module model = Module.dao.findById(getPara());
		setAttr("model", model);
		render(modulePath + "view.html");
	}

	/**
	 * 数据删除
	 */
	public void delete() {
		Module.dao.deleteById(getPara());
		list();
	}

	/**
	 * 跳转编辑页面
	 */
	public void edit() {
		Module model = Module.dao.findById(getPara());

		setAttr("model", model);
		setAttr("selectSystems", new SystemsService().selectSystems(model.getStr("systemsids")));
		setAttr("selectModule", new ModuleService().selectModule(model.getStr("parentmoduleids")));
		render(modulePath + "edit.html");
	}

	/**
	 * 保存
	 */
	public void save() {
		Module model = getModel(Module.class);
		String ids = getPara();
		if (ids == null) {
			model.put("ids", UtilsUtils.getRandomId(30));
			model.save();
		} else {
			model.update();
		}
		renderMessage("保存成功");
	}

	/**
	 * 编辑保存
	 */
	public void saveEdit() {
		Module model = getModel(Module.class);
		ModuleService.service.saveEdit(model);
		renderMessage("保存成功");
	}

	/**
	 *设置菜单 
	 */
	public void setMenu() {
		String moduleids = getPara();
		List<Menu> list = Menu.dao.findByWhere(" where 1=1 ");
		// 获取所有菜单
		String menuids = MenuService.service.getAllmenuids(moduleids);

		setAttr("ids", menuids);
		setAttr("menuids", menuids);
		// 根结点
		setAttr("list", list);
		render(modulePath + "menu.html");
	}


}
